<?php
namespace auth;
use think\Db;
use app\index\model\AuthRules;

class Auth 
{
    public static function check ($uid,$rule)
    {

       $info = AuthRules::get(['rule'=>strtolower($rule)]);

       if(!$info){
            return true;
       }

       $data = Db::table('fly_auth_user_role')
        ->alias('t1')
        ->field('*')
        ->join('fly_auth_role t2','t1.roid = t2.roid')
        ->where(['t1.uid'=>$uid])
        ->select();

        $rules = [];

        foreach ($data as $role) {
             $rules = array_merge($rules,explode(',',$role['rules']));
        }

        $rules  = array_unique($rules);

        $data = Db::table('fly_auth_rules')->where('ruid','in',$rules)->column('rule');

        if(in_array(strtolower($rule),$data)){
            return true;
        }else{
            return false;
        }


        // $data = AuthRules::all($rules);

        // var_dump($data);

        // var_dump($rules);

        // var_dump($data);

       // return true;
    }
}
